#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/3/17
# @USER    : Shengji He
# @File    : PalindromicSubstrings.py
# @Software: PyCharm
# @Version  : Python-
# @TASK: palindromic
"""
- Two Pointers
- String
- Dynamic Programming
"""


class Solution:
    """
    Given a string s, return the number of palindromic substrings in it.

    A string is a palindrome when it reads the same backward as forward.

    A substring is a contiguous sequence of characters within the string.

    Example 1:
        Input: s = "abc"
        Output: 3
        Explanation: Three palindromic strings: "a", "b", "c".

    Example 2:
        Input: s = "aaa"
        Output: 6
        Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

    Constraints:
        - 1 <= s.length <= 1000
        - s consists of lowercase English letters.
    """

    def countSubstrings(self, s: str) -> int:
        n = len(s)
        count = 0

        for i in range(n):
            left, right = i, i
            while left >= 0 and right < n and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1

            left, right = i, i + 1
            while left >= 0 and right < n and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1
        return count


if __name__ == '__main__':
    s = 'aaa'
    S = Solution()
    print(S.countSubstrings(s))
    print('done')
